package main

import (
	"fmt"
	"os"

	"github.com/ahviplc/GoJustToolc/UConsole"
	"github.com/spf13/viper"
)

type UserInfo struct {
	UserName string
	Address  string
	Sex      int
	Company  Company
}

type Company struct {
	Name       string
	EmployeeId string
	Department []interface{}
}

func main() {
	UConsole.Log("输出JAVA_HOME,结果如下:")
	getenv := os.Getenv("JAVA_HOME")
	fmt.Print(getenv) // 输出javahome
	UConsole.Log()
	UConsole.PrintAStraightLine()
	// 读取yaml文件
	v := viper.New()
	// 设置读取的配置文件名
	v.SetConfigName("config") // config.yaml 所以填写为config
	// 设置配置文件类型
	v.SetConfigType("yaml")
	// windows环境下为%GOPATH，linux环境下为$GOPATH
	v.AddConfigPath(".")

	if err := v.ReadInConfig(); err != nil {
		fmt.Printf("err:%s\n", err)
	}

	// fmt.Printf()
	fmt.Printf("userName:%s sex:%v company.name:%s \n", v.Get("userName"), v.Get("sex"), v.Get("company.name"))

	// 也可以直接反序列化为Struct
	var userInfo UserInfo
	if err := v.Unmarshal(&userInfo); err != nil {
		fmt.Printf("err:%s", err)
	}
	fmt.Println(userInfo)
	UConsole.PrintAStraightLine()

	// 打印
	fmt.Println(v.AllKeys())
	// fmt.Println(v.AllowEmptyEnv) // 0x7f2fc0
	UConsole.PrintAStraightLine()
	// fmt.Printf("GoToolcUrl:%s author:%s \n", v.Get("GoToolcUrl"), v.Get("author"))
	for k, v := range v.AllKeys() {
		fmt.Println(k, " -> ", v)
	}
	UConsole.PrintAStraightLine()
	allSettingsMap := v.AllSettings()
	fmt.Println(allSettingsMap)
	UConsole.PrintAStraightLine()
	for k, v := range allSettingsMap {
		fmt.Println(k, " -> ", v)
	}
	UConsole.PrintAStraightLine()
	UConsole.Log("循环遍历key为company的子Map,结果如下:")
	// 使用【.(map[string]interface{})】将【allSettingsMap["company"]】强制转成map
	// 使用类型断言 if companyMap, ok := allSettingsMap["company"].(map[int]interface{})
	// 如果 allSettingsMap["company"]是(map[int]interface{})类型的话,ok就是true,companyMap就是(map[int]interface{})类型allSettingsMap["company"]的值.
	// 否则ok为false，companyMap就是(map[int]interface{})类型的初始化 nil
	if companyMap, ok := allSettingsMap["company"].(map[string]interface{}); ok {
		UConsole.Log("类型转换成功:", ok)
		for k, v := range companyMap {
			fmt.Println(k, " -> ", v)
		}
	} else {
		UConsole.Log("无效的类型转换:", ok) // 如果无效类型转换 则 companyMap 会是初始值 nil
	}
	UConsole.PrintAStraightLine()

	// 输出JAVA_HOME,结果如下:
	// D:\Java\jdk1.8.0_211
	// -----------------------------------------------------------------------------------------------------
	// userName:LC sex:1 company.name:LC公司
	// {LC 上海 1 {LC公司 0001 [技术部]}}
	// -----------------------------------------------------------------------------------------------------
	// [address sex company.department company.name company.employeeid username]
	// -----------------------------------------------------------------------------------------------------
	// 0  ->  company.name
	// 1  ->  username
	// 2  ->  address
	// 3  ->  sex
	// 4  ->  company.employeeid
	// 5  ->  company.department
	// -----------------------------------------------------------------------------------------------------
	// map[address:上海 company:map[department:[技术部] employeeid:0001 name:LC公司] sex:1 username:LC]
	// -----------------------------------------------------------------------------------------------------
	// username  ->  LC
	// address  ->  上海
	// sex  ->  1
	// company  ->  map[department:[技术部] employeeid:0001 name:LC公司]
	// -----------------------------------------------------------------------------------------------------
	// 循环遍历key为company的子Map,结果如下:
	// 类型转换成功: true
	// department  ->  [技术部]
	// name  ->  LC公司
	// employeeid  ->  0001
	// -----------------------------------------------------------------------------------------------------
}
